#ifndef _LFS_LBD_H
#define _LFS_LBD_H

//the lumper block device abstraction
//LBD is a replicated, striped networked block device

#include <linux/types.h>
#include <linux/wait.h>
#include <linux/mutex.h>

#define LBD_SET_SOCK	_IO( 0xab, 0 )
#define LBD_SET_BLKSIZE	_IO( 0xab, 1 )
#define LBD_SET_SIZE	_IO( 0xab, 2 )
#define LBD_DO_IT	_IO( 0xab, 3 )
#define LBD_CLEAR_SOCK	_IO( 0xab, 4 )
#define LBD_CLEAR_QUE	_IO( 0xab, 5 )
#define LBD_PRINT_DEBUG	_IO( 0xab, 6 )
#define LBD_SET_SIZE_BLOCKS	_IO( 0xab, 7 )
#define LBD_DISCONNECT  _IO( 0xab, 8 )
#define LBD_SET_TIMEOUT _IO( 0xab, 9 )

enum {
	LBD_CMD_READ = 0,
	LBD_CMD_WRITE = 1,
	LBD_CMD_DISC = 2
};

#define lbd_cmd(req) ((req)->cmd[0])


/* values for flags field */
#define LBD_READ_ONLY 0x0001
#define LBD_WRITE_NOCHK 0x0002

struct request;

struct lbd_shard;

struct lbd_device {
	int flags;
	int harderror;		/* Code of hard error			*/
	struct socket * sock;
	struct file * file; 	/* If == NULL, device is not ready, yet	*/
	int magic;

	spinlock_t queue_lock;
	struct list_head queue_head;	/* Requests waiting result */
	struct request *active_req;
	wait_queue_head_t active_wq;
	struct list_head waiting_queue;	/* Requests to be sent */
	wait_queue_head_t waiting_wq;

	struct mutex tx_lock;
	struct gendisk *disk;
	int blksize;
	u64 bytesize;
	pid_t pid; /* pid of lbd-client, if attached */
	int xmit_timeout;
};


/* These are sent over the network in the request/reply magic fields */
#define LBD_REQUEST_MAGIC 0x19801024
#define LBD_REPLY_MAGIC 0x20051015

/*
 * This is the packet used for communication between client and
 * server. All data are in network byte order.
 */
struct lbd_request {
	__be32 magic;
	__be32 type;	/* == READ || == WRITE 	*/
	char handle[8];
	__be64 from;
	__be32 len;
} __attribute__((packed));

/*
 * This is the reply packet that lbd-server sends back to the client after
 * it has completed an I/O request (or an error occurs).
 */
struct lbd_reply {
	__be32 magic;
	__be32 error;		/* 0 = ok, else error	*/
	char handle[8];		/* handle you got from request	*/
};

typedef struct lbd_cmpx {
	u64	no;
	int strips;
	int replicas;
	int errors;

	struct lbd_device *members;
} lbd_t;


int lbd_init();
int lbd_cleanup();


#endif
